<?php
    include('include/header.inc.php');  // Nous appelons notre header
    $_SESSION['page'] = 'contact';
    include("include/sendMail.inc.php");

    $mailValid=false;

    if(isset($_POST) && count($_POST)>0)
    {
        // check if values have already been set
        if(empty($_POST['mail']) || empty($_POST['subject']) || empty($_POST['message']))
        {
            if(isset($_POST['mail']))
                $mail = $_POST['mail'];
            if(isset($_POST['subject']))
                $subject = $_POST['subject'];
            if(isset($_POST['message']))
                $message = $_POST['message'];
            echo "<script language='JavaScript'>alert('" . $texts['fill'][$_SESSION['lang']] . "')</script>";
            echo "<script language='JavaScript'>alert('fill')</script>";
        }
        else
        {
            $mail = $_POST['mail'];
            $subject = $_POST['subject'];
            $message = $_POST['message'];
            
            $subject = str_replace("\'", "'", $subject);
            $message = str_replace("\'", "'", $message);

            //Test the if the email addresse is valid
            $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';
            $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

            $regex = '/^' . $atom . '+' .
            '(\.' . $atom . '+)*' .

            '@' .
            '(' . $domain . '{1,63}\.)+' .

            $domain . '{2,63}$/i';

            if (preg_match($regex, $_POST['mail']))
                $mailValid = true;
            else
                echo "<script language='JavaScript'>alert('". $texts['invalid_email'][$_SESSION['lang']] ."')</script>";
            if($mailValid == true){
                sendMail($message,$mail,$subject);
                echo '<script>location.href="index.php"</script>';
            }
        }
    }
?>

    <div data-role="content" >	
        <H2><?php echo $texts['contact'][$_SESSION['lang']] ?></h2>
        <div class="tableauPage">
        <h2><?php echo $texts['contact_form'][$_SESSION['lang']] ?></h2>
        <form id="formContact" method="POST">
            
            <div data-role="fieldcontain">
                <label id="mailLabel" for="mail"><?php echo $texts['myemail'][$_SESSION['lang']] ?></label>
                <input id="mail" type="email" name="mail" 
                       <?php
                       // Retrieve the mail of the connected user
                       if ($_SESSION['mail'] != null)
                       {
                           echo 'readonly="readonly" value="'.$_SESSION['mail'].'"';
                       }
                       else
                       {
                           echo 'value="'.$mail.'"';
                       }
                       ?>
                       />
            </div>
            
            <div data-role="fieldcontain">
                <label id="subjectLabel" for="subject"><?php echo $texts['subject'][$_SESSION['lang']] ?></label>
                <input id="subject" type="text" name="subject" value="<?php echo $subject ?>"/>
            </div>

            <div data-role="fieldcontain">
                <label id="messageLabel" for="message"><?php echo $texts['message'][$_SESSION['lang']] ?></label>
                <textarea cols="40" rows="8" id="message" name="message"><?php echo $message ?></textarea>
            </div>  

            <center><input type="submit" value="<?php echo $texts['send'][$_SESSION['lang']] ?>" data-inline="true" /></center>
            
        </form>
       
    </div></div><!-- /content -->

    <?php
    if ($_SESSION['name'] <> '') {
        include('include/footer.inc.php');  // Nous appelons notre menu
    }
    else {
        ?>
        <!-- /footer -->        
    <div data-role="footer" data-theme="b" align="left" position="fixed">
        <!-- /navbar -->

        <div data-role="navbar">
            <ul>
                <li><a href="./index.php" rel="external"><img src="picture/home.png" /></a></li>

            </ul>
        </div>

    </div>
    </body>
</html>
    <?php
    }
    ?>
    